var onsaleTools = {};
//保存的数据
onsaleTools.SaveTempItem = [];
var test = 10;
var itemDialog = {};
//商品分页
itemDialog.search_item_function = function (S,d,P,key,pageSize){
	var $ = S.all;
	d.show();
	var pagination = null;
    //显示商品
    TOP.api("tql", "get", {ql:"select num_iid from taobao.items.onsale.get where q="+key,session:TOP.cookie.get("session")},function(response) {
    	var totalPage = response.items_onsale_get_response.total_results;
        var content = S.one('#page-content'),
        pagination = new P({
        	container: '#page',
            template: S.one('#default-pagination-tpl').html(),
            totalPage: Math.ceil(totalPage / pageSize),
            callback: function(idx, pg, ready) {
        		TOP.api("tql", "get", {ql:"select pic_url,title,num_iid,has_showcase,num,price,delist_time from taobao.items.onsale.get where page_size="+pageSize+" and page_no=" + idx+" and q="+key,session:TOP.cookie.get("session")},function(data) {
                    	var html = '';
                        S.each(data.items_onsale_get_response.items.item,function(item, i) {
                        	item.has_showcase = item.has_showcase ? "已推荐": "未推荐";
                            html += '<tr><td><input type="checkbox" name="ck_selectItem" value="' + item.num_iid + '"/></td>' + '<td style="padding-top: 8px;">' + '<img src="' + item.pic_url + '_40x40.jpg" alt="" width="40">' + '</td>' + '<td>' + item.price + '</td>' + '<td>' + item.num + '</td>' + '<td>' + item.delist_time + '</td>' + '<td>' + item.has_showcase + '</td>' + '<td><a href="http://item.taobao.com/item.htm?id=' + item.num_iid + '" target="_blank">' + item.title + '</a></td>' + '</tr>';
                        });
                        content.html(html);
                        ready(idx);
                        var flag = 0;
						//初始化数据
        				for (var i = 0; i < onsaleTools.SaveTempItem.length; i++) {
        					var list = $("#page-content input");
            				for (var j = 0; j < list.length; j++) {
            					if (onsaleTools.SaveTempItem[i] == list[j].value) {
            						list[j].checked = true;
                    				flag++;
                    				break;
               	 				}
            				}
            				//控制全选按钮
            				if(flag==list.length){
            					S.one('#ck_selectAll').prop("checked",true);
        					}else{
        						S.one('#ck_selectAll').prop("checked",false);
        					}
        				}
                        //反转控制
            		    S.all('#page-content input').on('change',function(event) {
            		    	itemDialog.funCheck(S,event.currentTarget);
            		    });
                     });
                   }
        });
	});
    //控制全选与反选
	S.one('#ck_selectAll').on('click',function(event) {
		$("#page-content input").prop("checked", event.currentTarget.checked);
	    //处理数据
	    var list = $("#page-content input");
	    for (var i = 0; i < list.length; i++) {
	    	itemDialog.saveItemorRemveItem(event.currentTarget.checked, list[i].value);
	    }
	});
	//搜索
	S.one('#bnt_searchItem').on('click',function(event){
    	var key = S.one("#itemkey").val();
    	itemDialog.search_item_function(S,d,P,key,8);
    });
}
//选择数据保存与删除
itemDialog.saveItemorRemveItem = function (b, id) {
	if (b) { //添加
		if (!itemDialog.isHaved(id)) {
			itemDialog.addItem(id);
		}
	} else { //删除
			if (itemDialog.isHaved(id)) {
				itemDialog.removeItem(id);
			}
	}
}
itemDialog.JS_cruel_search = function(key)       /*JS暴虐查找*/
{
	if(onsaleTools.SaveTempItem.length>0){
		var re = new RegExp(key,[""])
		return (onsaleTools.SaveTempItem.toString().replace(re,"┢").replace(/[^,┢]/g,"")).indexOf("┢")
	}
}
//查找是否存在数据
itemDialog.isHaved=function(id) {
	var result = itemDialog.JS_cruel_search(id);
	if(result>-1){
		return true;
	}
	else{
		return false;
	}
	/*var flag = false;
  		for (var i = 0; i < onsaleTools.SaveTempItem.length; i++) {
  			if (onsaleTools.SaveTempItem[i] == id) {
  				flag = true;
				break;
			}
  		}
		return flag;*/
}
//添加数据
itemDialog.addItem=function(id) {
	onsaleTools.SaveTempItem.push(id);
}
//删除数据
itemDialog.removeItem=function(id) {
	var result = itemDialog.JS_cruel_search(id);
	if(result>-1){
		onsaleTools.SaveTempItem.splice(result, 1);
	}
	/*for (var i = 0; i < onsaleTools.SaveTempItem.length; i++) {
		if (onsaleTools.SaveTempItem[i] == id) {
			onsaleTools.SaveTempItem.splice(i, 1);
		}
	}*/
}
itemDialog.funCheck=function(S,obj) {
	var $ = S.all;
	var list = $("#page-content input");
	var flag = true;
	for (var i = 0; i < list.length; i++) {
		if (!list[i].checked) {
			flag = false;
			break;
		}
	}
	itemDialog.saveItemorRemveItem(obj.checked, obj.value);
	$('#ck_selectAll').prop("checked", flag);
}